package com.java1234.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.java1234.dao.ISystemAdministratorDao;
import com.java1234.model.Librarian;
import com.java1234.model.SystemAdministrator;
import com.java1234.util.StringUtil;

public class SystemAdministratorImpl implements ISystemAdministratorDao {

	/**
	 * 登录验证
	 * 
	 * @param con
	 * @param system
	 * @return
	 * @throws Exception
	 */
	public SystemAdministrator login(Connection con, SystemAdministrator systemAdministrator) throws Exception {
		SystemAdministrator resultSystem = null;
		String sql = "select * from t_systemAdministrator where systemAdministratorAccount=? and password=?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, systemAdministrator.getSystemAdministratorAccount());
		pstmt.setString(2, systemAdministrator.getPassword());
		ResultSet rs = pstmt.executeQuery();
		if (rs.next()) {
			resultSystem = new SystemAdministrator();
			resultSystem.setId(rs.getInt("id"));
			resultSystem.setSystemAdministratorAccount(rs.getString("systemAdministratorAccount"));
			resultSystem.setPassword(rs.getString("password"));

		}
		return resultSystem;
	}

	/**
	 * 数据库系统管理员信息查询
	 * 
	 * @param con
	 * @param book
	 * @return
	 * @throws Exception
	 */
	public ResultSet list(Connection con, SystemAdministrator systemAdministrator) throws Exception {
		StringBuffer sb = new StringBuffer("select * from t_systemAdministrator");
		if (StringUtil.isNotEmpty(systemAdministrator.getSystemAdministratorAccount())) {
			sb.append(" and systemAdministratorAccount like '%" + systemAdministrator.getSystemAdministratorAccount()
					+ "%'");
		}
		if (StringUtil.isNotEmpty(systemAdministrator.getSystemAdministratorName())) {
			sb.append(" and systemAdministratorName like '%" + systemAdministrator.getSystemAdministratorName() + "%'");
		}
		if (StringUtil.isNotEmpty(systemAdministrator.getPassword())) {
			sb.append(" and password like '%" + systemAdministrator.getPassword() + "%'");
		}

		PreparedStatement pstmt = con.prepareStatement(sb.toString().replaceFirst("and", "where"));
		ResultSet i = pstmt.executeQuery();
		return i;
	}

	/**
	 * 数据库系统管理员信息添加
	 */
	public int add(Connection con, SystemAdministrator systemAdministrator) throws Exception {

		String sql = "insert into t_systemAdministrator values(?,?,?,?)";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setInt(1, systemAdministrator.getId());
		pstmt.setString(2, systemAdministrator.getSystemAdministratorAccount());
		pstmt.setString(3, systemAdministrator.getSystemAdministratorName());
		pstmt.setString(4, systemAdministrator.getPassword());

		int i = pstmt.executeUpdate();

		return i;

	}
	/**
	 * 数据库系统管理员信息修改
	 */
	public int update(Connection con, SystemAdministrator systemAdministrator) throws Exception {// 系统管理员信息修改
		String sql = "update t_systemAdministrator set systemAdministratorName=?,password=? where systemAdministratorAccount=?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, systemAdministrator.getSystemAdministratorName());
		pstmt.setString(2, systemAdministrator.getPassword());
		pstmt.setString(3, systemAdministrator.getSystemAdministratorAccount());
		int i = pstmt.executeUpdate();
		return i;
	}
	/**
	 * 数据库系统管理员信息删除
	 */
	public int delete(Connection con, String systemAdministratorAccount) throws Exception {// 系统管理员信息删除
		String sql = "delete from t_systemAdministrator where systemAdministratorAccount=?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, systemAdministratorAccount);
		int i = pstmt.executeUpdate();

		return i;
	}

}
